﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  abstract class SwingJointFunc : ItemFunc//子类可用
{
    protected bool IsClockWise;//类似private的bool 但可以给子类用
    /// <summary>
    /// 仅其绝对值起作用
    /// </summary>
    public float AngleSpeed;
    CombinableItem thisItem;//这个物体的变量类型
    private void Start()
    {
        thisItem = this.GetComponent<CombinableItem>();
    }
    public override void KeyOnInvoke()
    {
        base.KeyOnInvoke();//继承父类功能，现在没用
        if (IsClockWise) AngleSpeed = -Mathf.Abs(AngleSpeed);//Abs（）可求绝对值，为了顺时针旋转
        else AngleSpeed = Mathf.Abs(AngleSpeed);//逆时针旋转
        var name = GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().GetItemInJointDir(this.transform.name,2);//名字与物体绑定
        var ans = new List<CombinableItem>();
        thisItem.GetAllConnectedCombinableItemInDfs(ref ans);//根据连接情况找物体
        if (ans.Find(s => s.ItemFieldSetting.ItemName == this.transform.name) != null)//找了一圈找到自己
        {
            return;
        }//停
        this.GetComponent<SwingJoint>().downJointAngle += AngleSpeed * Time.fixedDeltaTime;
        thisItem.RotationInDegree -= AngleSpeed * Time.fixedDeltaTime / 2;
        if (string.IsNullOrEmpty(name) == false)//没找到
        {
            var item = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(name).GetComponent<CombinableItem>();//抄
            var list = new List<CombinableItem>();
            item.GetAllConnectedCombinableItemInDfs(ref list,thisItem);
            list.UniqueAdd<CombinableItem>(item);//找到与其相连的物体
            foreach(var x in list)//对于每个物体
            {
                x.RotateRelativeToPointInWorld(this.transform.position, AngleSpeed/2);//以当前物体位置中心旋转
            }
        }
        name= GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().GetItemInJointDir(this.transform.name, 0);
        if (string.IsNullOrEmpty(name) == false)//没找到
        {
            var item = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(name).GetComponent<CombinableItem>();//抄
            var list = new List<CombinableItem>();
            item.GetAllConnectedCombinableItemInDfs(ref list, thisItem);
            list.UniqueAdd<CombinableItem>(item);//找到与其相连的物体
            foreach (var x in list)//对于每个物体
            {
                x.RotateRelativeToPointInWorld(this.transform.position, -AngleSpeed/2);//以当前物体位置中心旋转
            }
        }

    }
}

